package net.sxnic.ugr.dept.action;

 
import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;

import net.sxinfo.core.dao.Page;
import net.sxinfo.core.dao.hibernate3.HibernateCriteria;
import net.sxinfo.core.dao.hibernate3.HibernateDaoUtils;
import net.sxinfo.core.dao.hibernate3.HibernateOrder;
import net.sxinfo.core.util.WebUtils;
import net.sxnic.comm.utils.PropertyUtil;
import net.sxnic.ugr.role.Role;

@SuppressWarnings("serial")
public class Browse extends DeptAction {

	private Page datas;
	
	private String deptCode;
	
	private String deptName;
	
	public String execute() throws Exception {
		
		try {
			checkRoles(Role.SYSTEM_ADMIN);

			int pageNumber = WebUtils.getPageNumber(request);
			int pageSize = PropertyUtil.findTableSize("firstbrowse.tabsize");

			HibernateOrder infoCodeOrder = HibernateDaoUtils
					.createHibernateOrder("deptOrder", true);
			HibernateCriteria hc = new HibernateCriteria().add(infoCodeOrder);

			HibernateOrder codeOrder = HibernateDaoUtils.createHibernateOrder(
					"code", true);
			hc.add(codeOrder);

			Criterion dc = null;

			dc = Restrictions.ne("id", "0");
			hc.add(dc);
			
			if(StringUtils.isNotBlank(deptCode)){
				dc = Restrictions.like("code",  "%" + deptCode + "%");
				hc.add(dc);
			}
			
			if(StringUtils.isNotBlank(deptName)){
				dc = Restrictions.like("name",  "%" + deptName + "%");
				hc.add(dc);
			}
			
			datas = deptManager.getPageByCriteria(pageNumber, pageSize, hc);

			System.out.println("datas.getTotalResults()=="
					+ datas.getTotalResults());

		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return SUCCESS;
	}

	public Page getDatas() {
		return datas;
	}

	public String getDeptName() {
		return deptName;
	}

	public void setDeptName(String deptName) {
		this.deptName = deptName;
	}

	public String getDeptCode() {
		return deptCode;
	}

	public void setDeptCode(String deptCode) {
		this.deptCode = deptCode;
	}


}
